﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Zyg.Utility.DBUtility;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Core.Providers.DALProviders
{
    public partial class MySqlProvider : DALProvider
    {
        #region Sql Statements

        private static readonly String SqlInsertLog = "INSERT INTO `Log` (`DateCreated`,`Message`,`Action`,`AccountID`,`ModuleID`) VALUES (@DateCreated,@Message,@Action,@AccountId,@ModuleId); set names utf8;";

        private static readonly String SqlSelectLogsAll = "SELECT `ID`,`DateCreated`,`Message`,`Action`,`AccountID`,`ModuleID` FROM `Log` ORDER BY `ID` DESC; set names utf8;";

        #endregion

        public override Zyg.SSO.Core.Business.Log SelectLog(int id)
        {
            return null;
        }

        public override void InsertLog(Zyg.SSO.Core.Business.Log log)
        {
            MySqlUtility db = new MySqlUtility();

            db.AddParameter("@DateCreated", MySqlDbType.DateTime, log.DateCreated);
            db.AddParameter("@Message", MySqlDbType.VarChar, log.Message);
            db.AddParameter("@Action", MySqlDbType.VarChar, log.Action);
            db.AddParameter("@AccountId", MySqlDbType.VarChar, log.AccountId);
            db.AddParameter("@ModuleId", MySqlDbType.VarChar, log.ModuleId);

            db.ExecuteSql(SqlInsertLog);
        }

        public override void DeleteLog(Zyg.SSO.Core.Business.Log log)
        {
            ;
        }

        public override void UpdateLog(Zyg.SSO.Core.Business.Log log)
        {
            ;
        }

        public override IList<Zyg.SSO.Core.Business.Log> FillLogs()
        {
            IList<Log> results = new List<Log>();

            MySqlUtility db = new MySqlUtility();

            MySqlDataReader reader = db.ExecuteSqlReader(SqlSelectLogsAll);

            if (reader != null)
            {
                while (reader.Read())
                {
                    Log log = new Log();

                    if (!reader.IsDBNull(0)) log.Id = reader.GetInt32(0);
                    if (!reader.IsDBNull(1)) log.DateCreated = reader.GetDateTime(1);
                    if (!reader.IsDBNull(2)) log.Message = reader.GetString(2);
                    if (!reader.IsDBNull(3)) log.Action = reader.GetString(3);
                    if (!reader.IsDBNull(4)) log.AccountId = reader.GetGuid(4);
                    if (!reader.IsDBNull(5)) log.ModuleId = reader.GetGuid(5);

                    log.MarkOld();

                    results.Add(log);
                }

                reader.Close();
            }

            return results;
        }
    }
}
